﻿using System;
using System.Configuration;
using System.Web;

namespace Salient.Web.Administration.AppConfig
{
    public partial class EditAppSetting : ApplicationConfigurationPage
    {
        private void Page_Load()
        {
            if (!IsPostBack)
            {
                string appPath = ApplicationPath;
                if (!String.IsNullOrEmpty(appPath))
                {
                    EditAppSettingTitle.Text = String.Format((string) GetLocalResourceObject("TitleForSite"), appPath);
                }

                AppSetting.Name = HttpUtility.HtmlDecode(Request["name"]);
                AppSetting.Value = HttpUtility.HtmlDecode(Request["value"]);
            }
        }

        public void Save(object sender, EventArgs e)
        {
            if (!IsValid)
            {
                return;
            }

            System.Configuration.Configuration config = OpenWebConfiguration(ApplicationPath);
            var appSettingsSection = (AppSettingsSection) config.GetSection("appSettings");
            string name = AppSetting.Name;
            string value = AppSetting.Value;
            if (appSettingsSection.Settings[name] == null)
                appSettingsSection.Settings.Add(name, value);
            else
                appSettingsSection.Settings[name].Value = value;
            SaveConfig(config);
            ReturnToPreviousPage(sender, e);
        }
    }
}